package mygang.security.authentication;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mygang.helper.JsonHelper;
import mygang.model.CustomResponse;
import mygang.model.UserWrapper;
import mygang.singleton.Statistic;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 07/08/2012
 */
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    public CustomAuthenticationSuccessHandler(){}

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        CustomResponse result = new CustomResponse(
                CustomResponse.Result.SUCCESS, 
                "hello", 
                "You have been successfully Logged In.", 
                "Success!");
        
        Object principal = authentication.getPrincipal();
        if(principal!=null && principal instanceof UserWrapper){
            Statistic.getInstance().users.add((UserWrapper)principal);
        }
        
        JsonHelper.prepareJsonResponse(result, response);
    }
}